package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ScheduleFileDao;
import cn.topevery.garden.entity.po.ScheduleFile;
import cn.topevery.garden.entity.query.ScheduleFileQuery;
import cn.topevery.garden.entity.vo.ScheduleFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-24
 */
@Repository
public class ScheduleFileDaoImpl extends BaseDaoImpl<ScheduleFile> implements ScheduleFileDao {
    @Override
    public <P extends DbCsp> List<ScheduleFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ScheduleFileVO> listPage(ScheduleFileQuery query) {
        String table = "t_schedule_file tsf";

        String columns = "tsf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tsf.c_id desc");
        sqlQueryMate
                .where(true, "tsf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ScheduleFileVO.class);
    }

    @Override
    public ScheduleFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<ScheduleFile> getListByPid(Integer id) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_schedule_file tsf").setColumns("tsf.*");
        sqlQueryMate.where(true, "tsf.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tsf.c_guid_id", ScriptConditionEnum.Eq, id.toString());
        return selectList(sqlQueryMate, ScheduleFile.class);
    }

    @Override
    public void deleteById(Integer id) {
        String sql = "delete from t_schedule_file where c_guid_id = " + id.toString();
        run(sql, CommandType.Text, dbCommand -> {
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public void deleteBatchByGuid(Integer userId, Integer[] ids) {
        List<String> idList = Arrays.stream(ids).map(Object::toString).collect(Collectors.toList());
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_schedule_file");
        sqlUpdateMate.setColumnValue(true, "c_db_last_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_last_update_id", userId)
                .setColumnValue(true, "c_db_status", 1)
                .where(true, "c_guid_id", ScriptConditionEnum.In, idList);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
